<template>
  <div class="history-module">
    <el-card class="history-card">
      <template #header>
        <div class="card-header">
          <span>我的收藏与点赞</span>
        </div>
      </template>
      <el-tabs>
        <el-tab-pane label="收藏">
          <div v-if="favorites.length === 0" class="empty-text">暂无收藏内容</div>
          <div v-else class="history-list">
            <div
              v-for="item in favorites"
              :key="item.id"
              class="history-item"
              @click="goToDetail(item)"
            >
              <div class="item-info">
                <span class="item-title">{{ item.title }}</span>
                <span class="item-type">{{ getTypeText(item.content_type) }}</span>
              </div>
              <span class="time">{{ formatTime(item.created_at) }}</span>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="点赞">
          <div v-if="likes.length === 0" class="empty-text">暂无点赞内容</div>
          <div v-else class="history-list">
            <div
              v-for="item in likes"
              :key="item.id"
              class="history-item"
              @click="goToDetail(item)"
            >
              <div class="item-info">
                <span class="item-title">{{ item.title }}</span>
                <span class="item-type">{{ getTypeText(item.content_type) }}</span>
              </div>
              <span class="time">{{ formatTime(item.created_at) }}</span>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import api from '@/api'
import { ElMessage } from 'element-plus'

const router = useRouter()
const favorites = ref([])
const likes = ref([])

// 获取收藏历史
const getFavorites = async () => {
  try {
    const response = await api.get('/users/favorites')
    favorites.value = response.data
  } catch (error) {
    console.error('获取收藏记录失败:', error)
    ElMessage.error('获取收藏记录失败')
  }
}

// 获取点赞历史
const getLikes = async () => {
  try {
    const response = await api.get('/users/likes')
    likes.value = response.data
  } catch (error) {
    console.error('获取点赞记录失败:', error)
    ElMessage.error('获取点赞记录失败')
  }
}

// 跳转到详情页
const goToDetail = (item) => {
  const routes = {
    course: '/course',
    event: '/event',
    knowledge: '/knowledge',
  }
  const route = routes[item.content_type]
  if (route) {
    router.push(`${route}/${item.content_id}`)
  }
}

// 获取内容类型文本
const getTypeText = (type) => {
  const types = {
    course: '课程',
    event: '活动',
    knowledge: '知识',
  }
  return types[type] || type
}

// 格式化时间
const formatTime = (time) => {
  return new Date(time).toLocaleString('zh-CN', {
    month: 'numeric',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  })
}

onMounted(() => {
  getFavorites()
  getLikes()
})
</script>

<style scoped>
.history-module {
  margin-bottom: 20px;
}

.history-card {
  background: var(--el-bg-color);
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  border-radius: 6px;
  background: var(--el-bg-color-page);
  cursor: pointer;
  transition: all 0.3s;
}

.history-item:hover {
  background: var(--el-color-primary-light-9);
  transform: translateX(4px);
}

.item-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.item-title {
  font-weight: 500;
  color: var(--el-text-color-primary);
}

.item-type {
  font-size: 12px;
  color: var(--el-text-color-secondary);
  background: var(--el-color-info-light-9);
  padding: 2px 8px;
  border-radius: 4px;
  width: fit-content;
}

.time {
  color: var(--el-text-color-secondary);
  font-size: 12px;
}

.empty-text {
  text-align: center;
  color: var(--el-text-color-secondary);
  padding: 20px 0;
}
</style>
